package com.ideaaedi.springcloud.jd.commonspring.validator;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * aop for {@link ValidId}
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
 * src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 2021.0.5.B
 */
@Slf4j
@Aspect
@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 89)
public class ValidIdAspect {
    
    @Around("@annotation(validId)")
    public Object aroundAdvice(ProceedingJoinPoint thisJoinPoint, ValidId validId) throws Throwable {
        Object[] arguments = thisJoinPoint.getArgs();
        if (arguments == null || arguments.length == 0) {
            return thisJoinPoint.proceed();
        }
        Object idObj = arguments[0];
        if (idObj instanceof Number) {
            SysValidator.verifyCouldDelThisId((Number)idObj);
        }
        return thisJoinPoint.proceed();
    }
    
}
